﻿using System;
using System . Collections . Generic;
using System . Linq;
using System . Text;
using System . Threading . Tasks;
using System . Xml . Linq;
using WenceyWang . RichMan4L . Buffs;

namespace WenceyWang . RichMan4L . Stocks
{
	/// <summary>
	/// 表示股票
	/// </summary>
	public class Stock : GameObject
	{
		/// <summary>
		/// 表示股票的名称
		/// </summary>
		public string Name { get; protected set; }

		/// <summary>
		/// 表示价格
		/// </summary>
		public double Price { get; set; }


		public List<long> PriceHistory { get; set; } = new List<long> ( );


		public List<Buffs . StockBuff> Buffs { get; set; } = new List<Buffs . StockBuff> ( );


		public StockPriceMovement Movement { get; set; }

		/// <summary>
		/// 今天的涨幅
		/// </summary>
		public double ChangeNet { get; set; }

		/// <summary>
		/// 预计明天的涨幅
		/// </summary>
		internal double nextDayChangeNet { get; set; }

		internal Calendars . GameDate movementChanging { get; set; } = Game . Current . Calendar . Today;

		internal long anticipatePrice { get; set; }

		void changeMovement ( )
		{
			switch ( Game . Current . Randomer . Next ( 0 , 3 ) )
			{
				case 1:
					{
						Movement = StockPriceMovement . Rise;
						break;
					}
				case 2:
					{
						Movement = StockPriceMovement . Keep;
						break;
					}
				case 3:
					{
						Movement = StockPriceMovement . Fall;
						break;
					}
			}
			ChangeAnticipate ( );
		}

		public void ChangeAnticipate ( )
		{
			double times = 1;
			switch ( Movement )
			{
				case StockPriceMovement . Rise:
					{
						times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMovementMax ) ) * 0.4 );
						break;
					}
				case StockPriceMovement . Fall:
					{
						times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMovementMin ) ) * 0.4 );
						break;
					}
			}
			switch ( Game . Current . StockMarket . Movement )
			{
				case StockPriceMovement . Rise:
					{
						times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMax ) ) * 0.4 );
						break;
					}
				case StockPriceMovement . Fall:
					{
						times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMin ) ) * 0.4 );
						break;
					}
			}
			switch ( Game . Current . GovermentControl )
			{
				case GovermentControl . Positive:
					{
						times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMax ) ) * 0.2 );
						break;
					}
				case GovermentControl . Negative:
					{
						times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMin ) ) * 0.2 );
						break;
					}
			}
			anticipatePrice = Convert . ToInt64 ( Convert . ToDouble ( Price ) * times );
			movementChanging += Game . Current . Randomer . Next ( 15 , 30 );
		}

		public override void EndToday ( )
		{

			ChangeNet = nextDayChangeNet;

			Price = Price * ChangeNet;

		}

		public override void StartDay ( Calendars . GameDate nextDate )
		{
			if ( Buffs . Any ( ( buff ) => { return ( buff is RedBuff ); } ) )
			{

			}
			else
			{
				if ( nextDate == movementChanging )
				{
					changeMovement ( );
				}
				nextDayChangeNet = ( ( anticipatePrice - Price ) * Game . Current . Randomer . Next ( 700 , 1300 ) / 1000d ) / ( movementChanging - nextDate ) / Price;
			}
			if ( Price <= 0 )
			{

				Delist ( );
			}
		}

		/// <summary>
		/// 使这个股票退市
		/// </summary>
		public void Delist ( )
		{

			StockDelistEvent?.Invoke ( this , new EventArgs ( ) );
		}

		public event EventHandler StockDelistEvent;


		public Stock ( ) : base ( )
		{

		}

		protected override void Dispose ( bool disposing )
		{
			if ( !disposedValue )
			{
				if ( disposing )
				{
					Game . Current?.StockMarket?.Stocks?.Remove ( this );
				}
				base . Dispose ( disposing );
			}
		}

	}
}
